package com.jyhg.core.mobile.effect
{
	import starling.animation.IAnimatable;
	import starling.display.Shape;
	import starling.display.Sprite;
	import starling.textures.RenderTexture;

	/**
	 * 闪电效果 
	 * @author lux
	 * 
	 */	
	public class ThunderEffect extends Sprite implements IAnimatable
	{
		private var _x1:Number;
		private var _x2:Number;
		private var _y1:Number;
		private var _y2:Number;
		
		private var _shape:Shape = new Shape();


		private var reTxteure:RenderTexture;
		
		private var _num:int = 20;				//闪电弯曲的数量
		public function ThunderEffect(x1:Number,y1:Number,x2:Number,y2:Number)
		{
			
			_x1 = x1;
			_x2 = x2;
			_y1 = y1;
			_y2 = y2;
			
			this.addChild(_shape)
		}
		
		public function  advanceTime(r:Number):void
		{
			run();
		}
		
		public function distroy():void
		{
			this.removeFromParent(true);
		}
			
		
		private function run():void {
			var xlen:int;
			var ylen:int;
			var tobj:*;
			xlen = (_x2-_x1)/_num;
			ylen = (_y2-_y1)/_num;
			_shape.graphics.clear();
			var pts:Array = [];
			for (var i:int = 0; i<_num; i++) {
				(i == _num-1) ? (tobj={x:_x2, y:_y2}) : (tobj={x:i*xlen+(_num*0.5-Math.random()*_num)+_x1, y:i*ylen+(_num*0.5-Math.random()*_num)+_y1});
				pts.push(tobj);
			}
			for (i = 1; i<pts.length; i++) {
				for (var j:int=0; j<=1; j++) {
					(j == 0) ? (_shape.graphics.lineStyle(5, 0x8984EE, 25)) : (_shape.graphics.lineStyle(2,0xFFFFFF, 100));
					_shape.graphics.moveTo(pts[i-1].x, pts[i-1].y);
					_shape.graphics.lineTo(pts[i].x, pts[i].y);
				}
			}
		}
	}
}